package com.example.chat;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

@Data
@Slf4j
public class ClientReceptionForServerRunnable implements Runnable {                                 //任务对象
    private String studentID;
    private String name;
    private Socket socket;
    public ClientReceptionForServerRunnable(String studentID, String name, Socket socket){
        this.studentID = studentID;
        this.name = name;
        this.socket = socket;
    }

    /**
     * 任务
     * 创建一个线程，用户客户端读取服务端发来的数据，TCP连接
     */
    @Override
    public void run() {
        try (
                DataInputStream dis = new DataInputStream(socket.getInputStream())
        ) {

            while (true){
                log.info(Thread.currentThread().getName());
                log.info("当前连接对象:{}，{},当前连接地址是：{}，当前连接端口是：{}，连接的地址是：{}，连接的端口是：{}",
                        name,studentID,socket.getLocalAddress(),socket.getLocalPort(),socket.getInetAddress(),socket.getPort());
                try {
                    String msg = dis.readUTF();
                    log.info("客户端读到数据  --->>>  数据：{}",msg);
                } catch (IOException e) {
                    System.out.println(socket.getRemoteSocketAddress()+"自己下线了！");
                    dis.close();
                    socket.close();
                    break;
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
